from PyQt4 import QtGui,QtCore
from ftllibs.FTL import ITEMS

## ItemMenu_Button
## The Buttons For the ItemMenu
class ItemMenu_Button(QtGui.QWidget):
    clicked = QtCore.pyqtSignal(['QString'])
    ItemIconHighLightEvent = QtCore.pyqtSignal([str])
    def __init__(self,itemid):
        super(ItemMenu_Button, self).__init__()
        self.itemid = str(itemid)
        self.setFixedSize(180,84)
        self.setContentsMargins(0,0,0,0)
        self.setStyleSheet('margin: 0px;border 0px')
        self.isEnabled=True
        self.initUI()

    def initUI(self):
        self.MasterLayout = QtGui.QGridLayout()
        self.PixMapLabel = QtGui.QLabel()
        # Icon
        self.ItemIcon=ITEMS.ITEMS[self.itemid].IconData.Default
        self.ItemIconGray=ITEMS.ITEMS[self.itemid].IconData.DefaultGrayScale
        #self.ItemIcon = QtGui.QPixmap("images/items/%s" % (ItemIcon)) # TODO:  Options parameter for item Icon PATH
        self.PixMapLabel.setPixmap(self.ItemIcon)
        # Name
        ItemName=ITEMS.ITEMS[self.itemid].Name
        self.ItemNameLabel=QtGui.QLabel()
        self.ItemNameLabel.setText("<h4>%s</h4>" % (ItemName))
        self.ItemNameLabel.setStyleSheet('color: Black')
        self.ItemNameLabel.setWordWrap(True)
        # TotalCosts
        ItemTotalCosts=str(ITEMS.ITEMS[self.itemid].Total_Costs)
        self.ItemTotalCostsLabel=QtGui.QLabel()
        self.ItemTotalCostsLabel.setText("<h5>%s</h5>" % (ItemTotalCosts))
        self.ItemTotalCostsLabel.setStyleSheet('color: red')
        # Layout
        self.MasterLayout.addWidget(self.PixMapLabel,0,0,2,1,QtCore.Qt.AlignLeft)
        self.MasterLayout.addWidget(self.ItemNameLabel,0,1,1,1,QtCore.Qt.AlignLeft)
        self.MasterLayout.addWidget(self.ItemTotalCostsLabel,1,1,1,1,QtCore.Qt.AlignRight)
        self.setLayout(self.MasterLayout)
        self.setToolTip(ITEMS.ITEMS[self.itemid].Description)
        
    def Enabled(self):
        self.isEnabled=True
        self.PixMapLabel.setPixmap(self.ItemIcon)
        self.update()
        
    def Disabled(self):
        self.isEnabled=False
        self.PixMapLabel.setPixmap(self.ItemIconGray)
        self.update()
            
    def mousePressEvent(self, event):
        if self.isEnabled:
            print "ItemMenu_Button: Emitting 'clicked':\"%s\"" % (self.itemid)
            self.clicked.emit(self.itemid)
        else:
            pass

    def mouseMoveEvent(self,e):
        if self.isEnabled:
            print "ItemMenu_Button: Emittig 'dragEvent':\"%s\"" % (self.itemid)
            mimeData = QtCore.QMimeData()
            mimeData.setData('text/plain',self.itemid)
            drag = QtGui.QDrag(self)
            drag.setMimeData(mimeData)
            drag.setPixmap(self.ItemIcon)
            drag.setHotSpot(e.pos() - self.rect().topLeft())
            drag.start(QtCore.Qt.IgnoreAction)

    def enterEvent(self,e):
        print "ItemIcon: EnterEvent: ",self.itemid
        self.ItemIconHighLightEvent.emit(self.itemid)


class ItemDetails(QtGui.QWidget):
    def __init__(self,parent=None):
        super(ItemDetails,self).__init__()
        self.MasterLayout = QtGui.QGridLayout()
        self.setFixedSize(480,200)
        self.StatsDescriptionLabel=QtGui.QLabel("hurga")
        self.MasterLayout.addWidget(self.StatsDescriptionLabel,0,0,1,1)
        self.setLayout(self.MasterLayout)

    def SetItemID(self,itemid):
        if self.sender().isEnabled:
            print "SetItemID Called :", itemid
            self.currentItemID=itemid
            if ITEMS.ITEMS[str(itemid)].RichText_Description != "":
                self.StatsDescriptionLabel.setTextFormat(1) 
                self.StatsDescriptionLabel.setText(QtCore.QString(ITEMS.ITEMS[str(itemid)].RichText_Description))
            else:
                self.StatsDescriptionLabel.setTextFormat(0)
                self.StatsDescriptionLabel.setText(QtCore.QString(ITEMS.ITEMS[str(itemid)].Description))
        else:
            self.StatsDescriptionLabel.setText('Not available on this map')
        self.update()


## ItemMenu
## These are All Items in the Game, split up in tabs for each tag
class ItemMenu(QtGui.QWidget):
    addItemPressed = QtCore.pyqtSignal(['QString'])
    def __init__(self,parent):
        super(ItemMenu,self).__init__()
        self.MasterLayout = QtGui.QVBoxLayout()
        self.setSizePolicy(QtGui.QSizePolicy.Fixed,QtGui.QSizePolicy.Expanding)
        self.setLayout(self.MasterLayout)
        self.setFixedWidth(440)
        self.AllButtons=[]
        self.initUI()
        
    def ChangeAvailability(self,indexnumber):
        if indexnumber == 0:
            for i in self.AllButtons:
                i.Enabled()
        if indexnumber == 1:
            for i in self.AllButtons:
                i.Disabled()
            for i in self.AllButtons: 
                if i.itemid in ITEMS.GetAvailabilityList('summonersrift'):
                    i.Enabled()
                

    def initUI(self):
        self.ItemAvailabilitySelect = QtGui.QComboBox()
        self.ItemAvailabilitySelect.addItem('All')
        self.ItemAvailabilitySelect.addItem('Summoners Rift')
        self.ItemAvailabilitySelect.addItem('Twisted Treeline')
        self.ItemAvailabilitySelect.addItem('Dominion')
        self.ItemAvailabilitySelect.setCurrentIndex(0)
        self.ItemAvailabilitySelect.currentIndexChanged[int].connect(self.ChangeAvailability)
        self.MasterLayout.addWidget(self.ItemAvailabilitySelect)
        self.ItemTabs = QtGui.QTabWidget()
        # defense
        self.DefenseTab = QtGui.QTabWidget()
        # defense -> health
        self.HealthScroll = QtGui.QScrollArea()
        self.HealthWidget = QtGui.QWidget()
        self.HealthWidget.setContentsMargins(0,0,0,0)
        self.HealthWidget.setStyleSheet('margin: 0px; border: 0px')
        self.HealthLayout = QtGui.QGridLayout()
        self.HealthLayout.setHorizontalSpacing(0)
        self.HealthLayout.setVerticalSpacing(0)

#        print self.AllButtons
        
        self.Buttons = []
        for i in ITEMS.GetList('health'):
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        print self.Buttons
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.HealthLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.HealthWidget.setLayout(self.HealthLayout)
        self.HealthScroll.setWidget(self.HealthWidget)
        # defense -> armor
        self.ArmorScroll = QtGui.QScrollArea()
        self.ArmorWidget = QtGui.QWidget()
        self.ArmorLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('armor'):
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
            
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.ArmorLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.ArmorWidget.setLayout(self.ArmorLayout)
        self.ArmorScroll.setWidget(self.ArmorWidget)
        # defense -> spell block (resist)
        self.ResistScroll = QtGui.QScrollArea()
        self.ResistWidget = QtGui.QWidget()
        self.ResistLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('spell_block'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.ResistLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.ResistWidget.setLayout(self.ResistLayout)
        self.ResistScroll.setWidget(self.ResistWidget)
        # defense -> Health Regen
        self.HealthRegenScroll = QtGui.QScrollArea()
        self.HealthRegenWidget = QtGui.QWidget()
        self.HealthRegenLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('health_regen'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.HealthRegenLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.HealthRegenWidget.setLayout(self.HealthRegenLayout)
        self.HealthRegenScroll.setWidget(self.HealthRegenWidget)
        # attack
        self.AttackTab = QtGui.QTabWidget()
        # attack -> damage
        self.DamageScroll = QtGui.QScrollArea()
        self.DamageWidget = QtGui.QWidget()
        self.DamageLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('damage'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.DamageLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.DamageWidget.setLayout(self.DamageLayout)
        self.DamageScroll.setWidget(self.DamageWidget)
        # attack -> Critical Strike
        self.CriticalStrikeScroll = QtGui.QScrollArea()
        self.CriticalStrikeWidget = QtGui.QWidget()
        self.CriticalStrikeLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('critical_strike'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.CriticalStrikeLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.CriticalStrikeWidget.setLayout(self.CriticalStrikeLayout)
        self.CriticalStrikeScroll.setWidget(self.CriticalStrikeWidget)
        # attack -> Attack Speed
        self.AttackSpeedScroll = QtGui.QScrollArea()
        self.AttackSpeedWidget = QtGui.QWidget()
        self.AttackSpeedLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('attack_speed'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.AttackSpeedLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.AttackSpeedWidget.setLayout(self.AttackSpeedLayout)
        self.AttackSpeedScroll.setWidget(self.AttackSpeedWidget)
        # attack -> Life Steal
        self.LifeStealScroll = QtGui.QScrollArea()
        self.LifeStealWidget = QtGui.QWidget()
        self.LifeStealLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('life_steal'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.LifeStealLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.LifeStealWidget.setLayout(self.LifeStealLayout)
        self.LifeStealScroll.setWidget(self.LifeStealWidget)
        # magic
        self.MagicTab = QtGui.QTabWidget()
        # magic -> spell_damage
        self.PowerScroll = QtGui.QScrollArea()
        self.PowerWidget = QtGui.QWidget()
        self.PowerLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('spell_damage'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.PowerLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.PowerWidget.setLayout(self.PowerLayout)
        self.PowerScroll.setWidget(self.PowerWidget)
        # magic -> CooldownReduction
        self.CRScroll = QtGui.QScrollArea()
        self.CRWidget = QtGui.QWidget()
        self.CRLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('cooldown_reduction'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.CRLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.CRWidget.setLayout(self.CRLayout)
        self.CRScroll.setWidget(self.CRWidget)
        # magic -> Mana
        self.ManaScroll = QtGui.QScrollArea()
        self.ManaWidget = QtGui.QWidget()
        self.ManaLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('mana'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.ManaLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.ManaWidget.setLayout(self.ManaLayout)
        self.ManaScroll.setWidget(self.ManaWidget)
        # magic -> Mana_Regen
        self.ManaRegenScroll = QtGui.QScrollArea()
        self.ManaRegenWidget = QtGui.QWidget()
        self.ManaRegenLayout = QtGui.QGridLayout()
        self.Buttons = []
        for i in ITEMS.GetList('mana_regen'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.ManaRegenLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.ManaRegenWidget.setLayout(self.ManaRegenLayout)
        self.ManaRegenScroll.setWidget(self.ManaRegenWidget)
        # Movement
        # Movement -> Movement
        self.MovementScroll = QtGui.QScrollArea()
        self.MovementLayout = QtGui.QGridLayout()
        self.MovementWidget = QtGui.QWidget()
        self.Buttons = []
        for i in ITEMS.GetList('movement'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.MovementLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.MovementWidget.setLayout(self.MovementLayout)
        self.MovementScroll.setWidget(self.MovementWidget)
        # Consumeables
        # Consumeables -> Consumeables
        self.ConsumScroll = QtGui.QScrollArea()
        self.ConsumLayout = QtGui.QGridLayout()
        self.ConsumWidget = QtGui.QWidget()
        self.Buttons = []
        for i in ITEMS.GetList('consumeable'):
            
            button = ItemMenu_Button(i)
            self.Buttons.append(button)
            self.AllButtons.append(button)
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 2:
                x = 0
                y += 1
            self.ConsumLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.ItemMenu_Button_clicked)
        self.ConsumWidget.setLayout(self.ConsumLayout)
        self.ConsumScroll.setWidget(self.ConsumWidget)
        #
        # build tab tree
        self.ItemTabs.addTab(self.DefenseTab,"Defense")
        self.DefenseTab.addTab(self.HealthScroll,"Health")
        self.DefenseTab.addTab(self.ResistScroll,"Spell Block")
        self.DefenseTab.addTab(self.HealthRegenScroll,"Health Regen")
        self.DefenseTab.addTab(self.ArmorScroll,"Armor")
        self.ItemTabs.addTab(self.AttackTab,"Attack")
        self.AttackTab.addTab(self.DamageScroll,"Damage")
        self.AttackTab.addTab(self.CriticalStrikeScroll,"Critical Strike")
        self.AttackTab.addTab(self.AttackSpeedScroll,"Attack Speed")
        self.AttackTab.addTab(self.LifeStealScroll,"Life Steal")
        self.ItemTabs.addTab(self.MagicTab,"Magic")
        self.MagicTab.addTab(self.PowerScroll,"Power")
        self.MagicTab.addTab(self.CRScroll,"Cooldown")
        self.MagicTab.addTab(self.ManaScroll,"Mana")
        self.MagicTab.addTab(self.ManaRegenScroll,"Mana Regen")
        self.ItemTabs.addTab(self.MovementScroll,"Movement")
        self.ItemTabs.addTab(self.ConsumScroll,"Consumable")
        self.MasterLayout.addWidget(self.ItemTabs)
        self.ItemDetails=ItemDetails(self)
        self.MasterLayout.addWidget(self.ItemDetails)
        for i in self.AllButtons:
                i.ItemIconHighLightEvent.connect(self.ItemDetails.SetItemID)
        # highlight event

    def ItemMenu_Button_clicked(self,itemid):
        print itemid
        self.addItemPressed.emit(itemid)

#######################################################
